#ifndef SERVO_MOTOR_H
#define SERVO_MOTOR_H

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

//帧头A
#define A_MT_HEAD   0xFA
//帧头B
#define B_MT_HEAD   0xFB
//方位匀速顺转
#define M_CMD_CST   0x01
//方位定点运行
#define M_CMD_H_DEST    0x02
//方位往复运行（先顺时针）
#define M_CMD_H_CWCIR     0x03
//方位速度设置
#define M_CMD_H_SPEED   0x04
//方位往复运行（先逆时针）
#define M_CMD_H_CCWCIR     0x05
//方位轴停止
#define M_CMD_H_STOP    0x06
//方位校零
#define M_CMD_H_CAL0    0x11
//方位回零
#define M_CMD_H_ORI     0x12
//方位寻0
#define M_CMD_H_FIND0  0xC1
//设置方位侧板打开时的温度阈值
#define M_CMD_TEMPHOLD  0x14
//打开方位风门
#define M_CMD_H_DAMPER    0X15
//方位加速度设置
#define M_CMD_H_ACCEL   0xb3
//开启正常协议
#define M_CMD_NORMAL  0xe1
//开启俯仰软限
#define M_CMD_V_SOFTLIMIT 0xd1
//关闭俯仰软限
#define M_CMD_V_NOLIMIT 0xd2
//俯仰软限减去上限角度
#define M_CMD_V_SUBUPLIMIT 0xd3
//俯仰软限减去下限角度
#define M_CMD_V_SUBDOWNLIMIT 0xd3


//方位匀速逆转
#define M_CMD_H_CCW     0x13

//俯仰定点运行
#define M_CMD_V_DEST    0xa2

//俯仰往复运行（先向上）
#define M_CMD_V_CWCIR     0xa3

///俯仰往复运行（先向下）
#define M_CMD_V_CCWCIR     0xa5

//俯仰速度设置
#define M_CMD_V_SPEED   0xa4

//俯仰轴停止
#define M_CMD_V_STOP    0xB4

//俯仰校零
#define M_CMD_V_CAL0    0xb1

//俯仰回零
#define M_CMD_V_ORI     0xb2

//打开伺服风门
#define M_CMD_ANTEDOOR  0xb5

//俯仰加速度设置
#define M_CMD_V_ACCEL   0xc3

//转台控制
#define H_OVER_LOAD 0x01
#define H_OVER_HEAT 0x02
#define H_ERR_ENCODE  0x04
#define V_OVER_LOAD 0x08
#define V_OVER_HEAT 0x10
#define V_ERR_ENCODE  0x20

//收藏角度
#define M_CMD_SAVE_H_ANGLE     0xd5
#define M_CMD_SAVE_V_ANGLE     0xd6
//运行到收藏角度
#define M_CMD_TO_H_ANGLE     0xd7
#define M_CMD_TO_V_ANGLE     0xd8


#pragma pack(push,1)
struct ServoMotorConfig
{
    struct servo_status
    {
        uint16_t    h_opera_loc;        //方位姿态数据
        uint16_t    h_opera_speed;      //方位速度数据
        uint16_t    v_opera_loc;        //俯仰姿态数据
        uint16_t    v_opera_speed;      //俯仰速度数据
        uint8_t     servo_fault;        //故障信息
        uint8_t     h_move_status;      //方位运动状态
        uint8_t     v_move_status;      //俯仰运动状态
        uint16_t    temperature;        //温度信息
        uint16_t    humidness;          //湿度信息
        uint16_t    atmospheric;        //大气压信息
        uint16_t    ecycle_loc;         //每圈转台位置
        uint8_t     ecycle_hours;      //每圈时间-时
        uint8_t     ecycle_min;        //每圈时间-分
        uint8_t     ecycle_sec;        //每圈时间-秒
        uint8_t     gps_status;        //GPS授时状态
        uint8_t     speed_regulation;    //是否调速
        uint32_t    x_inclinator;       //电子倾角仪X轴
        uint32_t    y_inclinator;       //电子倾角仪Y轴
        uint8_t     not_user0;          //预留
        uint8_t     not_user1;          //预留
        uint8_t     not_user2;          //预留
    }m_status;
    uint16_t servo_point_angle;         //指向固定角度单位为0.01度
    uint16_t servo_cw_speed;            //伺服正转转速设定单位为0.01°/s
    uint16_t servo_ccw_speed;           //伺服反转转速设定单位为0.01°/s
    uint16_t servo_accel;               //伺服加速度
    uint16_t servo_cycle_count;      //循环转动次数0为无限
    uint16_t servo_start_cycle_angle;   //转台起始转动角度-单位为0.01°
    uint16_t servo_end_cycle_angle;     //转台结束转动角度-单位为0.01°
    uint16_t servo_turn_switches;       //旋转开关0:停止;1顺转;2:逆转
    uint16_t servo_to_zero;             //伺服回0
    uint8_t servo_cali_0;               //伺服校0
    uint8_t fan_scan_mode;              //扇扫模式
    uint8_t servo_to_30;                //回转至30°
    uint8_t servo_cali_30;              //伺服校30
    uint16_t servo_temp_hold;           //设置方位侧板打开时的温度阈值
    uint16_t start_turn_hours;          //转动起始时间（时）
    uint16_t start_turn_min;            //转动起始时间（分）
    uint16_t start_turn_sec;            //转动起始时间（秒）

};
#pragma pack(pop)

class servoMotor
{
public:
    servoMotor();
    ~servoMotor() =  default;
private:

public:
    unsigned short int getModbusCRC16(unsigned char *_pBuf, unsigned short int _usLen);
    //伺服电机控制
    ServoMotorConfig    H_ConfigServo;//方位伺服
    ServoMotorConfig    V_ConfigServo;//俯仰伺服
    int servoPackage_1();
    int servoPackage_2();
    int servoPackage_3();
    int servoPackage_4();
    int servoPackage_5();
    int servoPackage_6();
    int servoPackage_11();
    int servoPackage_12();
    int servoPackage_13();
    int servoPackage_14();
    int servoPackage_15();
    int servoPackage_a2();
    int servoPackage_a3();
    int servoPackage_a4();
    int servoPackage_a5();
    int servoPackage_b1();
    int servoPackage_b2();
    int servoPackage_b3();
    int servoPackage_b4();
    int servoPackage_b5();
    int servoPackage_c3();
    int servoPackage_e1();
    int servoPackage_e2();
    int servoPackage_d5();
    int servoPackage_d6();
    int servoPackage_d7();
    int servoPackage_d8();

};

#endif